﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NWN.GDY.Service.Core;
using NWN.GDY.Service.Entity;
using Sdo.Payment.Common;
using NWN.GDY.Service.Common;

namespace NWN.GDY.Web
{
    public partial class ajax : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = "";
            string contenttype = "";
            try
            {
                string roomguid = Request["roomguid"];
                switch (Request["opName"])
                {
                    #region Register
                    case "Register":
                        string ruid = Request["uid"];
                        string rpsd = Request["psd"];
                        int iRegisterOut = new GDYEngine().RegisterUser(ruid, rpsd);
                        if (iRegisterOut == GDYConsts.ReturnDefine.SUCCESS)
                        { 
                            //设登录状态
                            LoginUser(ruid);
                        }
                        result = ToJSON((iRegisterOut));
                        contenttype = "application/json";
                        break;
                    #endregion
                    #region Login
                    case "Login":
                        string luid = Request["uid"];
                        string lpsd = Request["psd"];
                        int iLoginOut = new GDYEngine().UserLogin(luid, lpsd);
                        if (iLoginOut == GDYConsts.ReturnDefine.SUCCESS)
                        {
                            //设登录状态
                            LoginUser(luid);
                        }
                        result = ToJSON((iLoginOut));
                        contenttype = "application/json";
                        break;
                    #endregion
                    #region Logout
                    case "Logout":
                        FormsAuthentication.SignOut();
                        result = ToJSON((GDYConsts.ReturnDefine.SUCCESS));
                        contenttype = "application/json";
                        break;
                    #endregion
                    #region ToggleSeat
                    case "ToggleSeat":
                        string stid = Request["stid"];
                        bool seatdisabled = false;
                        int iToggleSeatOut = new GDYEngine().ToggleSeatDisable(roomguid, int.Parse(stid), Session.SessionID, out seatdisabled);
                        if (iToggleSeatOut == GDYConsts.ReturnDefine.SUCCESS)
                        {
                            result = "{\"result\":0,\"disabled\":" + seatdisabled.ToString().ToLower() + "}";
                        }
                        else
                            result = ToJSON((iToggleSeatOut));
                        contenttype = "application/json";
                        break;
                    #endregion
                    #region GetTable
                    case "GetTable":
                        GDY.Service.Entity.Room tOut;
                        if (!string.IsNullOrEmpty(roomguid))
                        {
                            tOut = new GDYEngine().GetRoom(roomguid);
                            result = XmlConfigMgr.ToXml(tOut);
                        }
                        //contenttype = "text/xml";
                        break;
                    #endregion
                    #region UserReady
                    case "UserReady":
                        string playerid = Request["playerid"];
                        int iUserReadyOut = new GDYEngine().PlayerReady(roomguid, playerid);
                        result = ToJSON((iUserReadyOut));
                        contenttype = "application/json";
                        break;
                    #endregion
                    #region GetRomms
                    case "GetRooms":
                        //先写死类型
                        List<NWN.GDY.Service.Entity.Room> Rooms = new GDYEngine().GetRooms(10);
                        if (Rooms == null)
                            Rooms = new List<NWN.GDY.Service.Entity.Room>();
                        result = XmlConfigMgr.ToXml(Rooms);
                        contenttype = "text/xml";
                        break;
                    #endregion
                    #region EnterRoom
                    case "EnterRoom":
                        string passport = Request["passport"];
                        if (!IsLogin)
                            result = ToJSON(GDYConsts.ReturnDefine.UserNotLogin);
                        else
                        {
                            int iEnterRoomOut = new GDYEngine().EnterRoom(CurrentUser, passport, roomguid);
                            result = ToJSON(iEnterRoomOut);
                        }
                        contenttype = "application/json";
                    break;
                    #endregion
                    default:
                        result = ToJSON((GDYConsts.ReturnDefine.AjaxException));
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result = ToJSON((GDYConsts.ReturnDefine.AjaxException));
            }
            Response.Clear();
            if (!string.IsNullOrEmpty(contenttype))
                Response.ContentType = contenttype;
            Response.Write(result);
            Response.End();
        }

        void LoginUser(string uid)
        {
            FormsAuthentication.SetAuthCookie(uid, true);
        }
    }
}